from marshmallow import Schema, fields, validate, ValidationError
from typing import Type, Dict, Any
from app.utils.errors import ValidationError as APIValidationError

class KnowledgeBaseSchema(Schema):
    name = fields.Str(required=True, validate=validate.Length(min=1, max=100))
    description = fields.Str()
    embedding_model = fields.Str(required=True, validate=validate.Length(max=50))

class ChatRequestSchema(Schema):
    query = fields.Str(required=True)
    model = fields.Str(load_default='llama2')
    system_prompt = fields.Str()

class SystemPromptSchema(Schema):
    name = fields.Str(required=True, validate=validate.Length(min=1, max=100))
    content = fields.Str(required=True)
    is_default = fields.Bool(load_default=False)

class DocumentSplitSchema(Schema):
    method = fields.Str(load_default='char', validate=validate.OneOf(['char', 'semantic', 'paragraph']))
    chunk_size = fields.Int(load_default=500, validate=validate.Range(min=100, max=2000))
    overlap = fields.Int(load_default=50, validate=validate.Range(min=0, max=200))

def validate_request(schema_cls: Type[Schema], data: Dict[str, Any]) -> Dict[str, Any]:
    """验证请求数据"""
    try:
        return schema_cls().load(data)
    except ValidationError as err:
        raise APIValidationError(err.messages) 